package web.controller;

import web.entity.PageMsg;
import web.entity.SUser;
import web.service.impl.UserServiceImpl;
import web.utils.Contains;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@WebServlet("/user.do")
public class UserServlet extends BaseServlet {
    private static UserServiceImpl userService = new UserServiceImpl();
    /**
     *
     */
    public void findPage(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        //获取到当前页的数据
        String current = req.getParameter("current");
        System.out.println("User-->"+current);
        int curr=1;
        if(current!=null){
            curr=Integer.parseInt(current);
        }
        System.out.println("当前页是："+curr);
        PageMsg<SUser> msg = userService.pageMsg(5, curr);
        req.getSession().setAttribute("msg1",msg);
        req.getRequestDispatcher("admin/user-list.jsp").forward(req,resp);
    }

    //生成验证码方法
    public void findYzm(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        System.out.println("user.do/findYzm方法");
        String yzm = userService.getYzm(req, res);

        req.getSession().setAttribute("yzm",yzm);
    }
    //登录方法(前台加后台)
    public void loginIn(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        System.out.println("user.do/loginIn方法");
        String username = req.getParameter("userName");
        String password = req.getParameter("passWord");
        String inputYzm = req.getParameter("inputYzm");
        String whe = req.getParameter("whe");

        HttpSession session = req.getSession();
        String yzm = (String)session.getAttribute("yzm");
        System.out.println(yzm+"--"+inputYzm);
        if ("backEnd".equals(whe)==false){
            if ((yzm.toLowerCase()).equals(inputYzm.toLowerCase())==false){
                System.out.println("验证码错误,登录失败");
                req.setAttribute("returnMsg","验证码错误,请重试！");
                req.getRequestDispatcher("admin/login.jsp").forward(req,res);
                return;
            }
        }
        System.out.println("req.getParameter获得的值:"+username+"--"+password);
        SUser theUser = new SUser(username, password);
        SUser user=userService.loginIn(theUser);
        //req.setAttribute("LoginUser",user);
        session.setAttribute("LoginUser",user);
        if (user==null){
                req.setAttribute("returnMsg","用户名或密码错误,请重试！");
                req.getRequestDispatcher("admin/login.jsp").forward(req,res);
        }else {
                req.getRequestDispatcher("admin/index.jsp").forward(req,res);
        }
    }

    //修改时先查询一个
    public void queryOne(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        String id = req.getParameter("id");
        SUser sUsers = userService.queryOne(id);
        req.getSession().setAttribute("sUsers",sUsers);
        req.getRequestDispatcher("user/update.jsp").forward(req,resp);
    }

    //修改一个用户
    public void updateOne(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        System.out.println("updateOne");
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String type = req.getParameter("type");
        String imgName = req.getParameter("imgName");
        int i = userService.modify(new SUser(id, username, password, Long.parseLong(type),imgName));
        resp.getWriter().write(Contains.URLL);
    }

    //添加一个用户
    public void add (HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String type = req.getParameter("type");
        String imgName = req.getParameter("imgName");
        String id= UUID.randomUUID().toString().replace("-","").substring(0,10);
        SUser user = new SUser(id,username,password,Long.valueOf(type),imgName);
        userService.add(user);
    }

    //删除一个用户， 先根据id查询对应的用户信息，转至用户删除页面
    public void  deleteFindById (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        SUser user=userService.deleteFindById(id);
        req.setAttribute("user",user);
        req.getRequestDispatcher("user/delete.jsp").forward(req,resp);
    }

    //根据提供的id进行删除信息
    public void cleanUp (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id=req.getParameter("id");
        userService.cleanUp(id);

    }


}
